<template>
  <div style="margin-bottom: 10px">
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form :model="query" ref="searchTagsRef">
            <el-form-item label="标签名称" label-width="70px">
              <el-input v-model="query.tagName" placeholder="请根据目录名称搜索"></el-input>
            </el-form-item>
            <el-form-item label="状态" label-width="70px">
              <el-select v-model="query.state" placeholder="请选择状态">
                <el-option v-for="item in options" :key="item.index" :label="item.label" :value="item.state"> </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-button style="margin-left: 120px" @click="clearSearch">清除</el-button>
          <el-button type="primary" @click="getTags">搜索</el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="success" icon="el-icon-edit" style="float: right" @click="showAddDialog">新增学科</el-button>
        </el-col>
      </el-row>
      <el-alert :title="dataNumber" type="info" show-icon> </el-alert>
      <el-table :data="query.items">
        <el-table-column label="序号" type="index" width="50px" align="center"></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column label="目录名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username" align="center"></el-table-column>
        <el-table-column label="创建日期" prop="addDate"></el-table-column>
        <el-table-column label="状态" prop="state" align="center">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.state === 1">已启用</el-tag>
            <el-tag type="danger" v-else>已屏蔽</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.state === 1" @click="turnBtn(scope.row.state, scope.row.id)">禁用</el-button>
            <el-button type="text" v-else @click="turnBtn(scope.row.state, scope.row.id)">启用</el-button>
            <el-button type="text" v-if="scope.row.state === 1" disabled>修改</el-button>
            <el-button type="text" v-else @click="showEditDialog(scope.row.id)">修改</el-button>
            <el-button type="text" v-if="scope.row.state === 1" disabled>删除</el-button>
            <el-button type="text" v-else @click="deleteTags(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page - 0"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="query.pagesize - 0"
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.counts"
      >
      </el-pagination>
    </el-card>
    <!-- 添加按钮对话框 -->
    <el-dialog title="添加标签" :visible.sync="addDialogFormVisible" width="400px">
      <el-form :model="addTagsForm" ref="addTagsFormRef">
        <el-form-item label="所属学科">
          <el-select placeholder="请选择学科" v-model="addTagsForm.subjectID">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称">
          <el-input autocomplete="off" v-model="addTagsForm.tagName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTags">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改按钮对话框 -->
    <el-dialog title="修改标签" :visible.sync="editDialogFormVisible" width="400px">
      <el-form :model="editTagsForm" ref="editTagsFormRef">
        <el-form-item label="所属学科">
          <el-select placeholder="请选择科目" v-model="editTagsForm.subjectID">
            <el-option v-for="item in subjectList" :key="item.id" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称">
          <el-input autocomplete="off" v-model="editTagsForm.tagName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editTags">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'tags',
  data () {
    return {
      query: {
        counts: 0,
        items: [],
        page: 1,
        pages: 0,
        pagesize: 10
      },
      options: [
        { state: 1, label: '已启用' },
        { state: 0, label: '已屏蔽' }
      ],
      // 添加标签表单
      addDialogFormVisible: false,
      addTagsForm: {
        subjectID: '',
        tagName: ''
      },
      // searchTagsForm: {},
      tagSelectId: '',
      editDialogFormVisible: false,
      editSubjectID: '',
      subjectList: [], // 学科列表
      editTagsForm: {}
    }
  },
  created () {
    this.getTags()
  },

  methods: {
    async getTags () {
      const { data: res } = await this.$http.get('/tags', { params: this.query })
      // console.log(res)
      this.query = res
      // console.log(this.query)
    },
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getTags()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.getTags()
    },
    clearSearch () {
      this.$refs.searchTagsRef.resetFields()
      this.getTags()
    },
    // 获取简单学科列表
    async getsubjectList () {
      // 获取简单学科列表
      const { data: res1 } = await this.$http.get('/subjects/simple')
      this.subjectList = res1
      // console.log(this.subjectList)
    },
    // 添加标签按钮
    async showAddDialog () {
      this.addDialogFormVisible = true
      this.getsubjectList()
    },
    addTags () {
      this.$refs.addTagsFormRef.validate(async valid => {
        if (!valid) return
        await this.$http.post('/tags', this.addTagsForm)
        // if (res.success !== true) return this.$message.error('添加标签失败！')
        // console.log(res)
        this.$message.success('添加标签成功！')
        this.addDialogFormVisible = false
        this.getTags()
      })
    },
    // 修改标签的状态
    async turnBtn (state, id) {
      var newState = state === 1 ? 0 : 1
      const { data: res } = await this.$http.post(`/tags/${id}/${newState}`)
      if (res.success !== true) return this.$message.error('变更状态失败！')
      this.$message.success('变更状态成功！')
      this.getTags()
    },
    // 修改标签按钮
    async showEditDialog (id) {
      this.editDialogFormVisible = true
      const { data: res } = await this.$http.get(`/tags/${id}`)
      this.editTagsForm = res
      // console.log(this.editTagsForm)
      this.getsubjectList()
    },
    // 确定修改标签
    editTags () {
      this.$refs.editTagsFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`/tags/${this.editTagsForm.id}`, this.editTagsForm)
        if (res.success !== true) return this.$message.error('修改标签失败！')
        this.$message.success('修改标签成功！')
        this.editDialogFormVisible = false
        this.getTags()
      })
    },
    // 删除标签
    deleteTags (id) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`/tags/${id}`)
        if (res.success !== true) return this.$message.error('删除失败！')
        this.$message.success('删除成功！')
        this.getTags()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  computed: {
    dataNumber () {
      return '数据一共' + this.query.items.length + '条'
    }
  }
}
</script>

<style scoped lang="less">
.el-card {
  padding: 20px;
}
.el-input {
  width: 200px;
}
.el-link {
  margin-right: 10px;
}
.el-pagination {
  margin-top: 20px;
  float: right;
}
</style>
